<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      /**
       * @param {string} s
       * @return {number}
       */
      var firstUniqChar = function (s) {
        /* for (var i = 0; i < s.length; i++) {
          for (var j = 0; j < s.length; j++) {
            // 找到非自己的相同字符直接跳出循环
            if (s[i] === s[j] && i !== j) break;
          }
          // 内部循环退出判断是break还是找到最后
          if (j === s.length) {
            return i;
          }
        }
        return -1 */
        
        for (let i = 0; i < s.length; i++) {
          if (s.lastIndexOf(s[i]) === s.indexOf(s[i])) {
            return i;
          }
        }
        return -1;
      };

      var s = "aabba";
      console.log(firstUniqChar(s));
    </script>
  </body>
</html>
